// pages/singerList/singerList.js
var app = getApp();
var util = require("../../utils/util");
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isMaskLoading: true, //  是否需要加载
    recData: [], //  选项卡数据
    defaultTabIndex: "0",
    rightFixData: [],
    defaultFixIndex: "0",
    defaultData: [], // list展示的数据
    extraTabShow: false, //tab栏是否展开显示
    topRpx: "", //固定定位距离顶部的rpx
    nowPositionName: "", //右侧字母导航点击显示的字母
    positionShow: false, //字母导航点击提示是否显示
    paramStr: "", //地区请求的字符串型请求头
    nowPage: "",
    last_page: "",
    tabName: "全部", //tab切换的名称
    rightshowName: "热", //右侧切换的名称
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    //  热门歌手
    var data = {
      per_page: 10,
      page: 1,
    };
    var dataStr = JSON.stringify(data);
    that.setData({
      paramStr: dataStr,
    });
    wx.request({
      url: util.api + "/singers?category=1",
      data: dataStr,
      method: "POST",
      success(res) {
        if (res.data.status_code == 200) {
          that.setData({
            defaultData: res.data.data.data,
          });
          //  歌手分类
          util.postRes(
            "/select/type",
            { code: "singer_type_mobile" },
            function (data) {
              that.setData({
                isMaskLoading: false,
                recData: data.data,
              });
            }
          );
        }
      },
    });
    that.setData({
      rightFixData: "热ABCDEFGHIJKLMNOPQRSTUVWXYZ".split(""),
    });
    // console.log(that.data.rightFixData, "999");
  },

  // 点击展开tab
  openTabClick: function name(params) {
    this.setData({
      extraTabShow: !this.data.extraTabShow,
    });
    // this.areaSinger();
  },
  // 关闭展开的tab
  closeTabClick() {
    this.setData({
      extraTabShow: !this.data.extraTabShow,
    });
  },

  /**
   * @description:　跳转到热门歌手详情页
   * @param        {[type]} event        [description]
   */
  bindNavView: function (event) {
    console.log(event);
    let id = event.currentTarget.dataset.id;
    let name = event.currentTarget.dataset.name;
    let imgurl = event.currentTarget.dataset.imgurl;
    wx.navigateTo({
      // url: "../singerIndex/singerIndex",
      url:
        "../singerIndex/singerIndex?id=" +
        id +
        "&name=" +
        name +
        "&imgurl=" +
        imgurl,
    });
  },
  singerSearch: function (params) {
    var data1 = JSON.stringify(this.data.defaultData);
    wx.navigateTo({
      url: "../singerSearch/singerSearch",
    });
  },

  /**
   * @description:　跳转到歌手分类详情页
   * @param        {[type]} event        [description]
   */
  getAlbumDetail: function (event) {
    var id = event.currentTarget.dataset.idx;
    var name = event.currentTarget.dataset.stname;
    console.log(id, name, "111");
    wx.navigateTo({
      url: "../../sheet/singers/singers-list?id=" + id + "&name=" + name,
    });
  },

  tabChange: function (e) {
    var that = this;
    console.log(e.currentTarget.dataset.idx);
    console.log(e.currentTarget.dataset.name);
    this.setData({
      defaultTabIndex: e.currentTarget.dataset.idx,
      extraTabShow: false,
      tabName: e.currentTarget.dataset.name,
    });
    that.tabRequest(e.currentTarget.dataset.idx);
    that.areaSinger(); //请求数据
  },
  // 根据tab切换,制作不同的请求头
  tabRequest(index) {
    var that = this;
    let params = {};
    let paramStr = "";
    that.setData({
      defaultFixIndex: "0",
      nowPositionName: "热",
    });
    switch (index) {
      case 0: // 全部
        params = {
          per_page: 10,
          page: 1,
        };
        console.log("1111111");
        paramStr = JSON.stringify(params);
        that.setData({
          paramStr: paramStr,
        });
        break;

      case 1: // 大陆男歌手
        paramStr = that.makeParams(1, 1);
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();
        break;

      case 2: // 大陆女歌手
        paramStr = this.makeParams(1, 2);
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();

        break;
      case 3: // 大陆组合
        paramStr = that.makeParams(1, 4);
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();

        break;
      case 4: // 港台男歌手
        paramStr = that.makeParamsIn("2,3", 1);
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();
        break;
      case 5: // 港台女歌手
        paramStr = that.makeParamsIn("2,3", 2);
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();
        break;
      case 6: // 港台组合
        paramStr = that.makeParamsIn("2,3", 4);
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();
        break;
      case 7: //欧美歌手
        paramStr = that.makeParamsIn(4, "1,2,4");
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();
        break;
      case 8: //日韩歌手
        paramStr = that.makeParamsIn("5,6", "1,2,4");
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();
        break;
      case 9: //其他综艺
        paramStr = that.makeParams(1, 4);
        that.setData({
          paramStr: paramStr,
        });
        // that.areaSinger();
        break;
    }
  },
  /**
   * @description:　右侧固定导航点击切换事件
   * @param        {[type]} event        [description]
   */
  rightChange: function (e) {
    let that = this;
    that.setData({
      defaultFixIndex: e.currentTarget.dataset.idx,
      nowPositionName: e.currentTarget.dataset.name,
      rightshowName: e.currentTarget.dataset.name,
    });
    Toast({
      duration: "500",
      message: e.currentTarget.dataset.name,
    });
    // console.log(that.data.paramStr);
    var index = that.data.defaultTabIndex; //tabindex
    var rightName = e.currentTarget.dataset.name; //右侧字母
    if (index == 0) {
      if (rightName == "热") {
        //当点击“热”时，进行默认请求
        var data = {
          per_page: 10,
          page: 1,
        };
        var dataStr = JSON.stringify(data);
        that.setData({
          paramStr: dataStr,
        });
        wx.request({
          url: util.api + "/singers?category=1",
          data: dataStr,
          method: "POST",
          success(res) {
            if (res.data.status_code == 200) {
              that.setData({
                defaultData: res.data.data.data,
              });
            }
          },
        });
        // that.tabRequest(index);
        console.log(222222222222);

        // that.setData({
        //   rightshowName: "热",
        // });
        // that.areaSinger();
      } else {
        console.log(index);
        console.log(JSON.parse(that.data.paramStr));
        var nowParamStr = JSON.parse(that.data.paramStr);
        nowParamStr.filter = [
          {
            matchType: "in",
            matchName: "areaType",
            matchValue: "1,2,3,4,5,6,7",
          },
          {
            matchType: "like",
            matchName: "pinyin_code",
            matchValue: rightName,
          },
        ];
        console.log(nowParamStr);
        that.setData({
          paramStr: JSON.stringify(nowParamStr),
        });
        util.singerArea("/singers", that.data.paramStr, function (res) {
          that.setData({
            defaultData: res.data.data,
            nowPage: res.data.current_page,
            last_page: res.data.last_page,
          });
        });
        // that.areaSinger();
        // {"matchType":"like","matchName":"pinyin_code","matchValue":"a"}
      }
    } else {
      if (rightName == "热") {
        //当点击“热”时，进行默认请求
        that.tabRequest(index);
        that.areaSinger();
      } else {
        console.log(index);
        console.log(JSON.parse(that.data.paramStr));
        var nowParamStr = JSON.parse(that.data.paramStr);
        nowParamStr.filter.push({
          matchType: "like",
          matchName: "pinyin_code",
          matchValue: rightName,
        });
        console.log(nowParamStr);
        that.setData({
          paramStr: JSON.stringify(nowParamStr),
        });
        that.areaSinger();
        // {"matchType":"like","matchName":"pinyin_code","matchValue":"a"}
      }
    }
  },
  // 不同地区的request请求头
  makeParams(area, type, page) {
    let initParams = {
      filter: [
        { matchType: "like", matchName: "areaType", matchValue: area },
        { matchType: "like", matchName: "type", matchValue: type },
      ],
      per_page: 20,
      page: page || 1,
    };
    // this.setData({
    //   pageNumber: page || 1,
    // });
    return JSON.stringify(initParams);
  },
  // 多个地区的request请求头，区别是like变成in
  makeParamsIn(area, type, page) {
    let initParams = {
      filter: [
        { matchType: "in", matchName: "areaType", matchValue: area },
        { matchType: "in", matchName: "type", matchValue: type },
      ],
      per_page: 10,
      page: page || 1,
    };
    this.setData({
      pageNumber: page || 1,
    });
    return JSON.stringify(initParams);
  },
  // 地区选择request    params用来判定是否是下拉触底更新
  areaSinger(params) {
    var that = this;
    if (params == "onReachBottom") {
      //下拉更新数据
      var initParams = JSON.parse(that.data.paramStr);
      initParams.page = that.data.nowPage + 1;
      if (that.data.defaultTabIndex == 0) {
        //如果是全部选项
        util.wholesingerArea("/singers", JSON.stringify(initParams), function (
          res
        ) {
          that.setData({
            defaultData: that.data.defaultData.concat(res.data.data),
            nowPage: res.data.current_page,
            last_page: res.data.last_page,
          });
        });
      } else {
        //不是全部选项
        util.singerArea("/singers", JSON.stringify(initParams), function (res) {
          that.setData({
            defaultData: that.data.defaultData.concat(res.data.data),
            nowPage: res.data.current_page,
            last_page: res.data.last_page,
          });
        });
      }
    } else {
      //不是底部刷新
      if (that.data.defaultTabIndex == 0) {
        //全部选项
        util.wholesingerArea("/singers", that.data.paramStr, function (res) {
          that.setData({
            defaultData: res.data.data,
            nowPage: res.data.current_page,
            last_page: res.data.last_page,
          });
        });
      } else {
        //不是全部选项
        util.singerArea("/singers", that.data.paramStr, function (res) {
          that.setData({
            defaultData: res.data.data,
            nowPage: res.data.current_page,
            last_page: res.data.last_page,
          });
        });
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;

    if (that.data.last_page > that.data.nowPage && that.data.last_page >= 2) {
      // console.log("2");
      that.areaSinger("onReachBottom");
    } else {
      // console.log("1");
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
